
const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {
    // onLoad () {}
    bottom: number = 0;
    top: number = 0;
    left: number = 0;
    right: number = 0;
    move: boolean = false;//逃跑中
    start() {
        this.top = cc.view.getVisibleSize().height / 2 - this.node.height / 2;
        this.bottom = -cc.view.getVisibleSize().height / 2 + this.node.height / 2;
        this.left = -cc.view.getVisibleSize().width / 2 + this.node.width / 2;
        this.right = cc.view.getVisibleSize().width / 2 - this.node.width / 2;
    }

    update(dt) {
        //如果主角碰到了我,我就换个位置逃跑
        let playerRect = cc.find('Canvas/player').getBoundingBox();
        if (!this.move) {//安全状态
            if (this.node.getBoundingBox().intersects(playerRect)) {//两个矩形相交
                //主角碰到我了
                this.move = true;//开始逃跑
                this.randomLocation();//重新生成位置
                // 分数加一
                cc.find('Canvas/分数/数字').getComponent('分数').updateNum(1);
            }
        }

    }
    randomLocation() {
        let x = Math.floor(Math.random() * this.right * 2) - this.right
        let y = Math.floor(Math.random() * this.top * 2) - this.top
        let self = this;
        cc.tween(this.node).to(1, { position: cc.v3(x, y, 0) }).call(function () {
            self.move = false;
        }).start()
        // this.node.x = x
        // this.node.y = y
    }
}
